//
//  DetailViewController.m
//  Travel_M_Travel
//
//  Created by dlios on 15-3-26.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "DetailViewController.h"
#import "NetworkingHandler.h"
#import "Album.h"
#import "Tracks.h"
#import "UIButton+InitButton.h"
#import "UIImageView+WebCache.h"
#import "AMBlurView.h"
#import "DetailCell.h"
#import "SDRefreshStart.h"

@interface DetailViewController ()<UITableViewDataSource, UITableViewDelegate, SDRefreshStartDelegate>
@property (nonatomic, retain)NSMutableArray *arr;
@property (nonatomic, retain)UITableView *tableView;
@property (nonatomic, retain)UIImageView *upImg;
@property (nonatomic, retain)UIView *headerView;
@property (nonatomic, retain)UIView *upView;
@property (nonatomic, retain)UILabel *upLabel;
@property (nonatomic, retain)UIImageView *smallImg;
@end

@implementation DetailViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.page = 1;
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self.navigationController setNavigationBarHidden:YES];

    NSString *str3 = [NSString stringWithFormat:@"%@", self.list.idNumber];
    
    NSString *str = [[@"http://mobile.ximalaya.com/mobile/others/ca/album/track/" stringByAppendingString:str3] stringByAppendingString:@"/true/1/30?device=iPhone"];
    
    
    [NetworkingHandler requestWithUrl:str completion:^(NSDictionary *resultDic) {
        
        
        NSDictionary *bigDic = [resultDic objectForKey:@"album"];
        self.album = [[Album alloc] initWithDic:bigDic];
        
        self.arr = [NSMutableArray array];
        NSArray *tmpArr = [[resultDic objectForKey:@"tracks"] objectForKey:@"list"];
        for (NSDictionary *dic in tmpArr) {
            Tracks *track = [[Tracks alloc] initWithDic:dic];
            [self.arr addObject:track];
            [track release];
        }
        
        [_tableView reloadData];
        NSLog(@"~~~~~~~~！！！！！！%@", self.arr);
        NSString *str1 = self.album.coverOrigin;
        str1 = [str1 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSURL *url = [NSURL URLWithString:str1];
        [_upImg setImageWithURL:url];
        
        NSString *str4 = self.album.coverSmall;
        str4 = [str4 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSURL *url2 = [NSURL URLWithString:str4];
        [_smallImg setImageWithURL:url2];
        
//        self.upLabel.text = self.album.title;
//        NSLog(@"label: ~~~~~%@", self.upLabel.text);
    }];
    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.view addSubview:self.tableView];
    [_tableView release];
    
     [SDRefreshStart SDRefeshTableView:self.tableView delegate:self];
    
    self.headerView = [[UIView alloc] initWithFrame: CGRectMake(0, 0, self.tableView.frame.size.width, self.view.frame.size.height*21/80)];
    self.tableView.tableHeaderView = _headerView;
    [_headerView release];
    
    self.upImg = [[UIImageView alloc] initWithFrame:CGRectMake(0, -self.view.frame.size.height/5, self.view.frame.size.width, self.view.frame.size.height*2/5)];
    self.upImg.backgroundColor = [UIColor yellowColor];
    self.upImg.userInteractionEnabled = YES;
    [self.headerView addSubview:_upImg];
    [_upImg release];
    
    self.smallImg = [[UIImageView alloc] initWithFrame:CGRectMake(40, 30, self.view.frame.size.width/5, self.view.frame.size.height/9)];
    self.smallImg.userInteractionEnabled = YES;
    [self.headerView addSubview:_smallImg];
    [_smallImg release];
    
    UIButton *play = [UIButton buttonWithType:UIButtonTypeSystem];
    play.frame = CGRectMake(22, 22, self.smallImg.frame.size.width/3, self.smallImg.frame.size.height/3);
    [play setBackgroundImage:[UIImage imageNamed:@"whiteplay.png"] forState:UIControlStateNormal];
    [play addTarget:self action:@selector(playAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [play backgroundImageForState:UIControlStateNormal];;
    [self.smallImg addSubview:play];
    
    AMBlurView *abv = [[AMBlurView alloc] initWithFrame:CGRectMake(0, 0, self.upImg.frame.size.width, self.upImg.frame.size.height)];
    abv.userInteractionEnabled = NO;
    abv.alpha = 0.85;
    [self.upImg addSubview:abv];
    [abv release];
    
    UIView *button3 = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height/5, self.view.frame.size.width, self.view.frame.size.height/16)];
    button3.backgroundColor = [UIColor whiteColor];
    [self.headerView addSubview:button3];
    
    UIButton *jj = [UIButton buttonWithType:UIButtonTypeSystem];
    jj.frame = CGRectMake(0, 0, self.view.frame.size.width/3, self.view.frame.size.height/16);
    [jj setTitle:@"简介" forState:UIControlStateNormal];
    [jj addTarget:self action:@selector(jjAction:) forControlEvents:UIControlEventTouchUpInside];
    [button3 addSubview:jj];
    
    UIButton *lb = [UIButton buttonWithType:UIButtonTypeSystem];
    lb.frame = CGRectMake(self.view.frame.size.width/3, 0, self.view.frame.size.width/3, self.view.frame.size.height/16);
    [lb setTitle:@"列表" forState:UIControlStateNormal];
    [lb addTarget:self action:@selector(lbAction:) forControlEvents:UIControlEventTouchUpInside];
    [button3 addSubview:lb];
    
    
    UIButton *xg = [UIButton buttonWithType:UIButtonTypeSystem];
    xg.frame = CGRectMake(self.view.frame.size.width*2/3, 0, self.view.frame.size.width/3, self.view.frame.size.height/16);
    [xg setTitle:@"相关专辑" forState:UIControlStateNormal];
    [xg addTarget:self action:@selector(xgAction:) forControlEvents:UIControlEventTouchUpInside];
    [button3 addSubview:xg];
    
    UIButton *back = [UIButton buttonWithType:UIButtonTypeSystem];
    back.frame = CGRectMake(50, 90, 50, 50);
    [back setTitle:@"back" forState:UIControlStateNormal];
    [back addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.upImg addSubview:back];
    
//    self.upView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.upImg.frame.size.height/4)];
//    self.upView.alpha = 0.1;
//    self.upView.backgroundColor = [UIColor blackColor];
//    [self.headerView addSubview:_upView];
//    [_upView release];
//    
//    self.upLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, self.view.frame.size.width-40, self.headerView.frame.size.height/5)];
//    self.upLabel.backgroundColor = [UIColor blueColor];
//    self.upLabel.text = @"dgh";
////    self.upLabel.text = self.album.title;
//    [_upView addSubview:_upLabel];
//    [_upLabel release];
}

- (void)playAction:(UIButton *)play
{

}

- (void)backAction:(UIButton *)back
{
    [self.navigationController popViewControllerAnimated:NO];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *str = @"reusen";
    DetailCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
    if (cell == nil) {
        cell = [[[DetailCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:str] autorelease];
    }
    Tracks *track = [self.arr objectAtIndex:indexPath.row];
    cell.track = track;
    NSLog(@"cell.track:!!!!!!%@", cell.track);
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}

- (void)jjAction:(UIButton *)jj
{
    
}

- (void)lbAction:(UIButton *)lb
{

}

- (void)xgAction:(UIButton *)xg
{

}

-(void)headRefesh
{
    
    self.page = 1;
    NSString *page = [NSString stringWithFormat:@"%ld", self.page];
    [self.arr removeAllObjects];
    
    NSString *str5 = [NSString stringWithFormat:@"%@", self.list.idNumber];
    
    NSString *str = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/others/ca/album/track/%@/true/%@/30?device=iPhone", str5, page];
    [NetworkingHandler requestWithUrl:str completion:^(NSDictionary *resultDic) {
        
        NSDictionary *bigDic = [resultDic objectForKey:@"album"];
        self.album = [[Album alloc] initWithDic:bigDic];
        
        self.arr = [NSMutableArray array];
        NSArray *tmpArr = [[resultDic objectForKey:@"tracks"] objectForKey:@"list"];
        for (NSDictionary *dic in tmpArr) {
            Tracks *track = [[Tracks alloc] initWithDic:dic];
            [self.arr addObject:track];
            [track release];
        }
        
        [_tableView reloadData];
        NSLog(@"~~~~~~~~！！！！！！%@", self.arr);
        NSString *str1 = self.album.coverOrigin;
        str1 = [str1 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSURL *url = [NSURL URLWithString:str1];
        [_upImg setImageWithURL:url];
        
        NSString *str4 = self.album.coverSmall;
        str4 = [str4 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSURL *url2 = [NSURL URLWithString:str4];
        [_smallImg setImageWithURL:url2];
        
        //        self.upLabel.text = self.album.title;
        //        NSLog(@"label: ~~~~~%@", self.upLabel.text);
    }];

    
}

- (void)footRefesh
{
    self.page++;
    NSString *page = [NSString stringWithFormat:@"%ld", self.page];
    
    
    NSString *str5 = [NSString stringWithFormat:@"%@", self.list.idNumber];
    
    NSString *str = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/others/ca/album/track/%@/true/%@/30?device=iPhone", str5, page];

    
    [NetworkingHandler requestWithUrl:str completion:^(NSDictionary *resultDic) {
        
        NSDictionary *bigDic = [resultDic objectForKey:@"album"];
        self.album = [[Album alloc] initWithDic:bigDic];
        
//        self.arr = [NSMutableArray array];
        NSArray *tmpArr = [[resultDic objectForKey:@"tracks"] objectForKey:@"list"];
        for (NSDictionary *dic in tmpArr) {
            Tracks *track = [[Tracks alloc] initWithDic:dic];
            [self.arr addObject:track];
            [track release];

        }
        
        [_tableView reloadData];
        NSLog(@"~~~~~~~~！！！！！！%@", self.arr);
        NSString *str1 = self.album.coverOrigin;
        str1 = [str1 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSURL *url = [NSURL URLWithString:str1];
        [_upImg setImageWithURL:url];
        
        NSString *str4 = self.album.coverSmall;
        str4 = [str4 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSURL *url2 = [NSURL URLWithString:str4];
        [_smallImg setImageWithURL:url2];
        
        //        self.upLabel.text = self.album.title;
        //        NSLog(@"label: ~~~~~%@", self.upLabel.text);
    }];

    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
